/**=========================================================
 * Service: datepicker-range.js
 =========================================================*/

App.service('DatepickerRange', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            init: function($scope, options) {
                var defaultOptions = {
                    optionModel: 'dateRange',
                    startDate: utils.addDate(new Date(), "day", -7),
                    endDate: utils.addDate(new Date(), "day", -1)
                }
                
                var finalOptions = angular.extend(defaultOptions,options)
                
                if (utils.isEmpty($scope.rangeDatePickers)){
                    $scope.rangeDatePickers = {}
                }
                
                $scope.rangeDatePickers[finalOptions.optionModel] = {};
                //开始日期
                $scope.rangeDatePickers[finalOptions.optionModel].startDate = {
                    date: finalOptions.startDate,
                    dateOptions: {
                        startingDay: 1,
                        maxDate: finalOptions.endDate
                    },
                    opened: false
                };
                //结束日期
                $scope.rangeDatePickers[finalOptions.optionModel].endDate = {
                    date: finalOptions.endDate,
                    dateOptions: {
                        startingDay: 1,
                        minDate: finalOptions.startDate
                    },
                    opened: false
                }
                
                return {
                    scope: $scope,
                    options: finalOptions,
                    addParam: function(params,format){
                        if (utils.isEmpty(format)){
                            format = 'yyyyMMdd';
                        }
                        params.startDate = utils.dateFormat(new Date($scope.rangeDatePickers[this.options.optionModel].startDate.date),format);
                        params.endDate = utils.dateFormat(new Date($scope.rangeDatePickers[this.options.optionModel].endDate.date),format);
                    },
                    getStartDate: function(){
                        return new Date($scope.rangeDatePickers[this.options.optionModel].startDate.date);
                    },
                    getEndDate: function(){
                        return new Date($scope.rangeDatePickers[this.options.optionModel].endDate.date);
                    }
                }
            }
        }
    }
]);
